Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented method getSalaryInfo(...) according to requirements #1334

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

opdhk
Copy link

@opdhk opdhk commented Sep 23, 2024

implemented method getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) according to requirements of README.MD

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! 👋 It looks like there's a critical issue with the getSalariesIndexByName method where it might not work correctly if the employeeName appears as a substring of another name. This could lead to incorrect index retrieval, which is pretty important to get right. So, let's fix that to ensure your code is matching names accurately. Keep up the good work, and with this change, your code will be even more robust! 💪

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

continue;
}
LocalDate parsedRecordDate = parseStringToLocalDate(records[DATE_INDEX]);
if (parsedRecordDate.isBefore(parseStringToLocalDate(dateFrom))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're parsing 'dateFrom' inside a loop, which is not necessary. You can parse it once before the loop starts, store it in a variable, and then use that variable inside the loop to compare with 'parsedRecordDate'. This will improve performance by avoiding repeated parsing of the same date.

if (parsedRecordDate.isBefore(parseStringToLocalDate(dateFrom))) {
continue;
}
if (parsedRecordDate.isAfter(parseStringToLocalDate(dateTo))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, 'dateTo' is also parsed inside the loop. It's more efficient to parse it once outside the loop and use the parsed value for comparison inside the loop.

Comment on lines 61 to 63
StringBuilder reportBuilder = new StringBuilder(REPORT_HEADER).append(dateFrom)
.append(REPORT_DELIMITER)
.append(dateTo)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'REPORT_DELIMITER' constant is used to separate 'dateFrom' and 'dateTo' in the report. However, it is also hardcoded on line 66. To maintain consistency and avoid magic strings, you should use 'REPORT_DELIMITER' on line 66 as well.

reportBuilder.append(System.lineSeparator());
}
}
return String.valueOf(reportBuilder);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use 'String.valueOf(reportBuilder)' since 'reportBuilder' is already a StringBuilder and calling 'toString()' on it will give you the string representation. You can simply return 'reportBuilder.toString()'.

return composeReport(names, dateFrom, dateTo, salaries);
}

private static int getSalariesIndexByName(String indexedNameString, String employeeName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method 'getSalariesIndexByName' might not work correctly if the 'employeeName' appears in the indexedNameString as a substring of another name. This could lead to incorrect index retrieval. You should consider adding delimiters to ensure that the exact name is matched.

* parsing dateFrom and dateTo transferred outside the loop;
* hardcoded delimiter ' - ' changed to constant;
* employee name with delimiters used to prevent situation for incorrect index search if employee name is a substring for another employee name
* recommendation to change stringBuilder.toString() instead of String.valueOf(stringBuilder) ignored
Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on addressing the core functionality of your code! 🌟 You've handled the main tasks well. For future improvements, consider enhancing your error messages with actual input values to make debugging easier, and use more idiomatic methods like toString() for converting StringBuilder to String. Keep up the good work! 👍

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

reportBuilder.append(System.lineSeparator());
}
}
return String.valueOf(reportBuilder);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using String.valueOf(reportBuilder), you can simply use reportBuilder.toString() to convert the StringBuilder to a String. This is more idiomatic and direct.

Integer.parseInt(records[INCOME_INDEX]) * Integer.parseInt(
records[HOURS_INDEX]);
} catch (NumberFormatException e) {
throw new RuntimeException(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'Wrong format of working hours or income per hour in records line.' could be more informative. Include the actual data that caused the error to make debugging easier for users or other developers.

try {
return Integer.parseInt(indexSalaries);
} catch (NumberFormatException e) {
throw new RuntimeException("Wrong index format in indexed string of employee names.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, the error message 'Wrong index format in indexed string of employee names.' could include the actual string that failed to parse to aid in debugging.

private static final String INDEX_NAME_PAIRS_DELIMITER = "|";
private static final String REPORT_DELIMITER = " - ";
private static final String REPORT_HEADER = "Report for period ";

public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
final LocalDate fromDate = LocalDate.parse(dateFrom, inputFormatter);
final LocalDate toDate = LocalDate.parse(dateTo, inputFormatter);
StringBuilder report = new StringBuilder("Report for period ")
.append(fromDate.format(inputFormatter))
.append(" - ")
.append(toDate.format(inputFormatter));
int[] salaries = new int[names.length];
for (String info : data) {
String[] parts = info.split(" ");
LocalDate recordDay = LocalDate.parse(parts[DATE_INDEX], inputFormatter);
if ((recordDay.isAfter(fromDate) || recordDay.isEqual(fromDate))
&& (recordDay.isBefore(toDate) || recordDay.isEqual(toDate))) {
for (int i = 0; i < names.length; i++) {
if (names[i].equals(parts[NAME_INDEX])) {
salaries[i] += Integer.parseInt(parts[HOURS_INDEX])
* Integer.parseInt(parts[INCOME_INDEX]);
}
}
}
}
for (int i = 0; i < names.length; i++) {
report.append(System.lineSeparator())
.append(names[i])
.append(" - ")
.append(salaries[i]);
}
return report.toString();
}

Copy link

@ahoienko ahoienko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, left some comments

Comment on lines 17 to 42
final LocalDate fromDate = LocalDate.parse(dateFrom, FORMATTER);
final LocalDate toDate = LocalDate.parse(dateTo, FORMATTER);
StringBuilder report = new StringBuilder(REPORT_HEADER).append(dateFrom)
.append(REPORT_DELIMITER)
.append(dateTo);
int[] salaries = new int[names.length];
for (String info : data) {
String[] parts = info.split(DATA_DELIMITER);
LocalDate recordDay = LocalDate.parse(parts[DATE_INDEX], FORMATTER);
if ((recordDay.isAfter(fromDate) || recordDay.isEqual(fromDate)) && (
recordDay.isBefore(toDate) || recordDay.isEqual(toDate))) {
for (int i = 0; i < names.length; i++) {
if (names[i].equals(parts[NAME_INDEX])) {
salaries[i] += Integer.parseInt(parts[HOURS_INDEX]) * Integer.parseInt(
parts[INCOME_INDEX]);
}
}
}
}
for (int i = 0; i < names.length; i++) {
report.append(System.lineSeparator())
.append((names[i]))
.append(REPORT_DELIMITER)
.append(salaries[i]);
}
return report.toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is vert complicated, I suggest you to split it into several parts, for example create method that will create report (1), method that will identify is date correct (2) , method that will pars salary (3)

Comment on lines +36 to +41
for (int i = 0; i < names.length; i++) {
report.append(System.lineSeparator())
.append((names[i]))
.append(REPORT_DELIMITER)
.append(salaries[i]);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1)

Comment on lines 26 to 27
if ((recordDay.isAfter(fromDate) || recordDay.isEqual(fromDate)) && (
recordDay.isBefore(toDate) || recordDay.isEqual(toDate))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2)

Comment on lines 28 to 33
for (int i = 0; i < names.length; i++) {
if (names[i].equals(parts[NAME_INDEX])) {
salaries[i] += Integer.parseInt(parts[HOURS_INDEX]) * Integer.parseInt(
parts[INCOME_INDEX]);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(3)

@opdhk opdhk requested a review from ahoienko September 26, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants